/**
 @Name：login
 @Author：zhangjie
 @version：1.0.0
 @Deprecated：login 接口类
 */
import request from '@/api/request/axios.ts';
import {getRefreshToken, setTokenInfo} from '@/utils/token.ts';

/**
 * 验证码
 */
export const captcha = (key: string) => request({
  url: '/without/sys/code/captcha/' + key,
  method: 'get'
});

/**
 * 登录
 * @param data
 */
export const login = (data: any) => {
  const formData = new FormData();
  formData.append('username', data.username);
  formData.append('password', data.password);
  formData.append('grant_type', 'password');
  return request.post('/login', formData, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic ZmVycmV0LXdlYi1jbGllbnQ6MTIzNDU2'
    },
  }).then((res: any) => {
    // 保存access_token、refresh_token信息
    setTokenInfo(res.data['access_token'], res.data['refresh_token']);
    // tokenStore.setAccessToken(res.data['access_token'], res.data['refresh_token']);
    return res;
  });
};

/**
 * 刷新token
 */
export const refresh = () => {
  const formData = new FormData();
  formData.append('refresh_token', getRefreshToken());
  formData.append('grant_type', 'refresh_token');
  return request.post('/login', formData, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic ZmVycmV0LXdlYi1jbGllbnQ6MTIzNDU2'
    },
  }).then((res: any) => {
    // 保存access_token、refresh_token信息
    setTokenInfo(res.data['access_token'], res.data['refresh_token']);
    return res;
  });
};

/**
 * 获取用户信息
 */
export const usersInfo = () => request({
  url: '/sys/users/info',
  headers: {
    gotoLogin: false
  },
  method: 'get'
}).then((res: any) => {
  // 保存 user 信息
  // userStore.setUserInfo(res.data);
  return res;
});
